package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go.uber.org/zap"
	"homepage_srv/homepage"
	"homepage_srv/internal/dao"
	"homepage_srv/internal/svc"
	"smart-health-platforms/pkg"
)

type GetCommentsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCommentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCommentsLogic {
	return &GetCommentsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询评论详情
func (l *GetCommentsLogic) GetComments(in *homepage.GetCommentsRequest) (*homepage.GetCommentsResponse, error) {
	// 调用修改后的函数获取评论列表
	commentList, err := dao.GetCommentList(in)
	if err != nil {
		pkg.Error("评论列表展示失败",
			zap.Int64("good_id", in.GoodId), zap.Error(err))
		return nil, err
	}
	// 转换为 protobuf 类型的评论列表
	comments := make([]*homepage.Comment, len(commentList))
	for i, commentDetail := range commentList {
		comments[i] = &homepage.Comment{
			Id:        commentDetail.ID,
			GoodId:    commentDetail.GoodId,
			UserId:    commentDetail.UserId,
			Content:   commentDetail.Content,
			CreatedAt: commentDetail.CreatedAt.Unix(),
			Replies:   make([]*homepage.Reply, len(commentDetail.Replies)),
		}

		// 转换回复列表
		for j, reply := range commentDetail.Replies {
			comments[i].Replies[j] = &homepage.Reply{
				CommentId: reply.CommentId,
				UserId:    reply.UserId,
				Content:   reply.Content,
				CreatedAt: reply.CreatedAt.Unix(),
			}
		}
	}
	// 返回包含评论列表的响应
	return &homepage.GetCommentsResponse{
		Comment: comments, // 注意字段名可能需要调整
	}, nil
}
